#region Using

using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

#endregion

public partial class login : System.Web.UI.Page
{
    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        string returnUrl = Request.QueryString["ReturnUrl"];
        //string wikiReturnUrl = Request.QueryString["Redirect"];
        //if (wikiReturnUrl != null)
        //    returnUrl = "../wiki/" + wikiReturnUrl;
        string createUserUrl = "register.aspx";
        if (returnUrl != null)
            createUserUrl += "?ReturnUrl=" + returnUrl;

        Button LoginButton = Login1.FindControl("LoginButton") as Button;
        Login1.FailureText = "We're sorry, login was unsuccessful.  Please try again.  Remember, passwords are case-sensitive.<br /><br />If you have not yet created a Just Ask Users account, you can <a href=\"" + createUserUrl + "\">create one here.</a>";

        SetActiveButtonAttributes(LoginButton);
        if (Page.User.Identity.IsAuthenticated)
        {
            if (returnUrl != null)
                Response.Redirect(returnUrl);
            else
                Response.Redirect("../default.aspx", true);
        }
        else
        {
            Login1.LoggedIn += new EventHandler(Login1_LoggedIn);
            Login1.FindControl("username").Focus();
        }
    }

    private void SetActiveButtonAttributes(Button button)
    {
        button.Attributes.CssStyle.Add("background-color", "#eee");
        button.Attributes.Add("OnMouseOut", "this.style.backgroundColor ='#eee';");
        button.Attributes.CssStyle.Add("text-decoration", "none");
        button.Attributes.CssStyle.Add("margin", "3px");
        button.Attributes.CssStyle.Add("padding", "7px 6px 7px 6px");
        button.Attributes.Add("OnMouseOver", "this.style.backgroundColor ='#ddd';");
    }

    /// <summary>
    /// Handles the ContinueButtonClick event of the changepassword1 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    void changepassword1_ContinueButtonClick(object sender, EventArgs e)
    {
        Response.Redirect(Request.RawUrl, true);
    }

    /// <summary>
    /// Handles the LoggedIn event of the Login1 control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    void Login1_LoggedIn(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["CSUserProfile"];
        if (cookie == null)
            cookie = new HttpCookie("CSUserProfile");

        MembershipUser user = Membership.GetUser(Login1.UserName);
        cookie["Email"] = user.Email;
        cookie["CommonName"] = Profile.Personal.DisplayName;
        cookie["EnableDisplayName"] = "true";

        Response.Cookies.Add(cookie);

        string returnUrl = Request.QueryString["ReturnUrl"];

        if (returnUrl != null)
            Response.Redirect(returnUrl);
        else
            Response.Redirect("../default.aspx", true);

    }
}
